//
//  QYGMineDollController.m
//  wawaji_ios
//
//  Created by 云+物娱 on 2017/12/8.
//  Copyright © 2017年 yiguo qi. All rights reserved.
//

#import "QYGMineDollController.h"
#import "QYGNetRequestModel.h"
#import "QYGMineFetchCell.h"
#import "QYGGameRecordModel.h"

static NSString *mineDollCell = @"mineDollCell";
@interface QYGMineDollController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger page;

@end

@implementation QYGMineDollController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cm_backgroudColor];
    self.title = @"我的娃娃";
    [self setupUI];
    [self setupData];
}
#pragma mark- tableviewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ScaleW(75);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QYGGameRecordModel *gameRecordModel = self.dataArray[indexPath.row];
    QYGMineFetchCell *cell = [tableView dequeueReusableCellWithIdentifier:mineDollCell forIndexPath:indexPath];
    [cell fetCellWithIconStr:gameRecordModel.ware_image nameStr:gameRecordModel.ware_name detailStr:gameRecordModel.created_at fetchCellMode:FetchCellModeMineDoll accessoryStr:nil accessColor:[UIColor clearColor] isPick:gameRecordModel.ware_status == 2 ? YES : NO];
    return cell;
}


#pragma mark - setData

- (void)setupData{
    [self.tableView.mj_header beginRefreshing];
}
#pragma mark - setUI
- (void)setupUI{
    
    [self.view addSubview:self.tableView];
    
    WS(weakSelf);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadNewData];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadMoreData];
    }];
}

#pragma mark - loadData
- (void)loadDataWithPage:(NSInteger)page{
    NSInteger per_page = 20;
    [QYGNetRequestModel getMineDollRecordsWithPage:page per_page:per_page Completed:^(NSArray *dollRecordArr) {
        [self endRefresh];
        [self.dataArray addObjectsFromArray:dollRecordArr];
        [self.tableView reloadData];
        self.page = page;
    } WithErrorCodeBlock:^(NSError *error) {
        [self endRefresh];
    }];
}
- (void)endRefresh{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

- (void)loadMoreData{
    [self loadDataWithPage:self.page + 1];
}

- (void)loadNewData {
    self.dataArray = [NSMutableArray array];
    [self loadDataWithPage:1];
}


#pragma mark - lazy

- (UITableView *)tableView {
    if(_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        CGFloat navH = self.navigationController.navigationBar.height;
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, navH + 20, 0);
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        
        [_tableView registerClass:[QYGMineFetchCell class] forCellReuseIdentifier:mineDollCell];
    }
    return _tableView;
}

@end
